<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * M_verificationlink
 *
 * This is an M_resendverificationlink of a few basic verificationlink interaction methods you could use
 * all done with a hardcoded array.
 *
 * @package		CodeIgniter
 * @subpackage	Rest Server
 * @category	Controller
 * @author		Phil Sturgeon
 
*/

// This can be removed if you use __autoload() in config.php OR use Modular Extensions
require APPPATH.'/libraries/REST_Controller.php';

class Openpin extends REST_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->model('mdl_getbalance');
		
	}

   /* function verificationlink_post()
    {
        //$this->some_model->updateverificationlink( $this->get('id') );
     
		
$data['username']=$this->post('username'); 
$data['password']=$this->post('password'); 
$data['level_id']=$this->post('level_id'); 



		$query = $this->mdl_verificationlink->insert($data);
		
	
		if ($query){
			
			$this->response(array('status' => 'true'),200);
		}else{
			$this->response(array('status' => 'false'));
		}

    }
	*/
	
	function check_openpin_post(){
		      

		//post
		$mobilephone=$this->post('mobilephone'); 
		$username=$this->post('username'); 
		$tokenonapps = $this->post('token'); 
		
		$arrwhere="username = '" . $username."'" ;
		$query = $this->mdl_getbalance->get_item(0, 0, $arrwhere);
		$result =$query->result_array();
		$token = $result[0]['token'];
		
	
		$this->load->helper('jwt_helper');
		$token = JWT::decode($tokenonapps, 'goopay');
		$mobilephonetoken= $token->mobilephone;
        $usernametoken= $token->username;
		
		if($mobilephonetoken==$mobilephone && $username==$usernametoken) {
				$this->load->helper('string');
				$this->load->helper('text');
				$digit1='';
				$digit2='';
				while($digit1==$digit2) {
				$digit1 = mt_rand(1, 6);
				$digit2 = mt_rand(1, 6);		
				}
				$data = array(
				'digit1'=>$digit1,
				'digit2'=>$digit2
				);
				
				$query = $this->mdl_getbalance->updaterandomdigit($username,$data);
		
				$openpinform[] = array(
				
				'openpinformstatus' => 'true',
				'formtitle' => 'PIN',
				'formlabel' => 'Masukkan PIN Anda digit ke '.$digit1.' dan '.$digit2,
				'token'=>$result[0]['token']
				);
			
				}
		else {
	
				$openpinform[] = array(
				
				'openpinformstatus' => 'false',
				'debug' => 'belum terdaftar di goopay',
				'token'=>'false'
				);
	
	}
		
						
			
			
			
			 $this->response($openpinform, 200);	
			
	
	}
	
	function send_pin_post(){
		  $this->load->helper('text');    

		//post
		$mobilephone=$this->post('mobilephone'); 
		$username=$this->post('username'); 
		$random2pin = $this->post('random2pin');
		$amount = $this->post('amount');
		$channelid = $this->post('channelid');
		$terminalid = $this->post('terminalid');
		$referenceid = $this->post('referenceid');
		$tokenonapps = $this->post('token');
		
		$arrwhere="username = '" . $username."'" ;
		$query = $this->mdl_getbalance->get_item(0, 0, $arrwhere);
		$result =$query->result_array();
		
		$balance = $result[0]['balance'];
		$pin = $result[0]['pin'];
		$digit1 = $result[0]['digit1'];
		$digit2 = $result[0]['digit2'];
		
		
		$pin1 = substr($pin,$digit1-1,1);
		$pin2 = substr($pin,$digit2-1,1);
		$pin=$pin1.$pin2;
		$this->load->helper('jwt_helper');
		$token = JWT::decode($tokenonapps, 'goopay');
		$mobilephonetoken= $token->mobilephone;
        $usernametoken= $token->username;
		
		
		if($mobilephonetoken==$mobilephone && $username==$usernametoken) {
				if($random2pin==$pin) {
				$send_pin[] = array(
				'send_pinstatus' => 'true',
				'send_pinnotofication' => 'SUKSES',
				'pin'=>$pin,
				'token'=>$result[0]['token']
				);
				
				
				$this->load->helper('date');
				$datestring = "%Y-%m-%d %h:%i:%s";
				$time = time();
				$trxdate=mdate($datestring, $time);
				
				$timeid = "%Y%m%d%h%i%s";
				$time = time();
				$ts=mdate($timeid, $time);
				$refid = random_string('numeric', 4);		
				$transactionid = $ts.$refid;		
				
				$balance = $balance - $amount;
	
				$data = array(
				'username' => $username,
				'mobilephone' => $mobilephone,
				'balance'=>$balance,
				'amount'=>$amount,
				'trxdate'=>$trxdate,
				'channelid'=>$channelid,
				'terminalid'=>$terminalid,
				'referenceid'=>$referenceid,
				'transactionid'=>$transactionid
				
				);
								
				$query = $this->mdl_getbalance->insert($data);
				
				
				$query = $this->mdl_getbalance->updatebalance($username,$data);
		
				

				} else {

				$send_pin[] = array(
				'send_pinstatus' => 'false',
				'send_pinnotofication' => 'Wrong PIN',
				'pin'=>$pin,
			
				'token'=>$result[0]['token']
				);
				}		
				
			
				}
		else {
	
				$send_pin[] = array(
				
				'send_pinstatus' => 'false',
				'debug' => 'belum terdaftar di goopay',
				'token'=>'false'
				);
	
	}
		
						
			
			
			
			 $this->response($send_pin, 200);	
			
	
	}
    /*
    function verificationlink_delete()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        $message = array('message' => 'DELETED!', 'id'=>$this->delete('id'));
		
		$query = $this->mdl_verificationlink->delete($this->delete('id'));
		//$this->response($message,200);
		//$query=1;
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
	/*
	 function verificationlink_put()
    {
		
    	//$this->some_model->deletesomething( $this->get('id') );$this->_get('id')
        //$message = array('message' => 'UPDATE!', 'userid'=>$this->delete('userid'), 'name'=>$this->delete('name'));
		$data['userid']=$this->put('userid'); 
$data['username']=$this->put('username'); 
$data['password']=$this->put('password'); 
$data['level_id']=$this->put('level_id'); 

		$query = $this->mdl_verificationlink->update($data['userid'],$data);
		//$this->response($message,200);
		
		if ($query){
			$this->response(array('status' => 1),200);
		
		}else{
			$this->response(array('status' => 0));
		}
      
    } */
    /*
    function verificationlinks_get()
    {
        //$verificationlinks = $this->some_model->getSomething( $this->get('limit') );
        $verificationlinks = array(
			array('id' => 1, 'name' => 'Some Guy', 'email' => 'example1@example.com'),
			array('id' => 2, 'name' => 'Person Face', 'email' => 'example2@example.com'),
			3 => array('id' => 3, 'name' => 'Scotty', 'email' => 'example3@example.com', 'fact' => array('hobbies' => array('fartings', 'bikes'))),
		);
        
        if($verificationlinks)
        {
            $this->response($verificationlinks, 200); // 200 being the HTTP response code
        }

        else
        {
            $this->response(array('error' => 'Couldn\'t find any items!'), 404);
        }
    } */
/*

	public function send_post()
	{
		var_dump($this->request->body);
	}
*/
/*
	public function send_put()
	{
		var_dump($this->put('foo'));
	}
	*/
}